package com.graduation.sports;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import com.graduation.sports.bean.CommunityInfo;
import com.graduation.sports.bean.DailyExerciseInfo;
import com.graduation.sports.bean.EveryDaySteps;
import com.graduation.sports.bean.GroupInfo;
import com.graduation.sports.bean.SportDiaryBean;
import com.graduation.sports.bean.SportRecordBean;
import com.graduation.sports.bean.UserInfo;
import com.graduation.sports.config.Session;
import com.graduation.sports.db.BaseDBHelper;
import com.graduation.sports.db.DataManager;

import org.litepal.LitePal;
import org.litepal.LitePalDB;

import io.realm.Realm;

/**
 * Created by apc_allen
 * date : 2/16/21
 * description :
 */
public class IApplication extends Application {
    private static IApplication applicationContext;
    private static Handler handler;

    DataManager dataManager;
    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        initDB();

        Realm.init(getInstance());

        Session.initSportMap(getApplicationContext());
//        dataManager = new DataManager(new RealmHelper());
//
//        if (dataManager.isFirstInit()) {
//            dataManager.initTextData();
//        }

    }

    public static IApplication getInstance() {
        return applicationContext;
    }

    public static Handler getHandler() {
        if (null == handler)
            handler = new Handler(Looper.getMainLooper());
        return handler;
    }


    private void initDB(){
        LitePal.initialize(this);

        LitePalDB litePalDB = new LitePalDB("graduationSports", 5);
        litePalDB.addClassName(UserInfo.class.getName());
        litePalDB.addClassName(SportRecordBean.class.getName());
        litePalDB.addClassName(GroupInfo.class.getName());
        litePalDB.addClassName(CommunityInfo.class.getName());
        litePalDB.addClassName(DailyExerciseInfo.class.getName());
        litePalDB.addClassName(EveryDaySteps.class.getName());
        litePalDB.addClassName(SportDiaryBean.class.getName());

        LitePal.use(litePalDB);

        if (BaseDBHelper.getInstance().isFirstInit()) {
            BaseDBHelper.getInstance().initTextData();
        }
    }
}
